<template>
  <div class="app-container">
    <el-button @click="addDialogFormVisible = true">添加一级分类</el-button>

    <el-dialog title="添加" :visible.sync="addDialogFormVisible">
      <el-form ref="form" label-width="120px">
        <el-form-item label="输入标签名">
          <el-input v-model="newName"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addFC">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="updateVisible">
      <el-form ref="updateForm" :model="updateForm" label-width="120px">
        <el-form-item label="输入标签名">
          <el-input v-model="updateForm.name"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updFC">确 定</el-button>
      </div>
    </el-dialog>

    <el-table v-loading="loading" :data="firCla" stripe style="width: 100%; margin-top: 30px;">
      <el-table-column prop="id" label="#" width="200"/>
      <el-table-column prop="name" label="名称" width="250">
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="danger" @click="delFC(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { getFirCla, delFirCla, addFirCla, updFirCla } from '@/api/classification'

export default {
  data() {
    return {
      loading: false,
      updateVisible: false,
      addDialogFormVisible: false,
      firCla: [],
      newName: '',
      updateForm: {
        id: '',
        name: ''
      }
    }
  },
  mounted() {
    this.getFC()
  },
  methods: {
    // 获取一级标签
    getFC() {
      this.loading = true
      getFirCla().then(res => {
        this.firCla = res.data
        this.loading = false
      })
    },

    // 添加一级标签
    addFC() {
      addFirCla(this.newName).then(res => {
        this.getFC()
        this.$message('添加成功!')
        this.addDialogFormVisible = false
      })
    },

    delFC($id) {
      delFirCla($id).then(res => {
        this.getFC()
        this.$message('删除成功!')
      })
    },

    handleUpdate($obj) {
      this.updateForm.id = $obj.id
      this.updateForm.name = $obj.name
      this.updateVisible = true
    },

    updFC() {
      updFirCla(this.updateForm).then(res => {
        this.getFC()
        this.$message('修改成功!')
        this.updateVisible = false
      })
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>
